package com.webservice.command.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {
	
	/**
	 * 目录是否存在
	 * @param dirName
	 * @return
	 */
	public boolean checkDir(String dirName){
		File dir = new File(dirName);
		if(dir.exists()){
			 return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 创建目录
	 * @param dirName
	 * @return
	 */
	public boolean createDir(String dirName){
        File dir = new File(dirName);
        
        if (!dirName.endsWith(File.separator)) {
        	dirName = dirName + File.separator;
        }
        //创建目录
        if (dir.mkdirs()) {
            System.out.println("创建目录" + dirName + "成功！");
            return true;
        } else {
            System.out.println("创建目录" + dirName + "失败！");
            return false;
        }
    }
	
	/**
	 * 创建目录
	 * @param dirName
	 * @return
	 */
	public boolean createDirectory(String dirName){
		boolean checkFlag = checkDir(dirName);
		if(!checkFlag){
			boolean createFlag = createDir(dirName);
			return createFlag;
		}else{
			return checkFlag;
		}
		
    }
	
	/**
     * 写文件
     */
    public void wirteFile(String fileName, String content) {
        try {
            //打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            FileWriter writer = new FileWriter(fileName, false);
            writer.write(content);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
	
	/**
     * 读取文件
     */
	public String readFile(String fileName) {
		StringBuffer sb = new StringBuffer();
		File file = new File(fileName);
		BufferedReader reader = null;
		try {
			String encoding = CharacterEnding.getFileCharacterEnding(file);
			reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(file), encoding));
			String tempString = null;
			int line = 1;

			while ((tempString = reader.readLine()) != null) {
				sb.append(tempString);
				line++;
			}
			return sb.toString();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException ie) {
					ie.printStackTrace();
				}
			}
		}
		return null;
	}
	
	 /**
	 * 查询某个目录下的所有文件
	 */
	public List<String> getAllFile(String strPath) {
		List<String> fileList = new ArrayList<String>();
		return refreshFileList(strPath, fileList);
	}

	/**
	 * 遍历文件夹里的内容
	 */
	public static List<String> refreshFileList(String strPath,  List<String> fileList) { 
   	
		File dir = new File(strPath); 
		File[] files = dir.listFiles(); 
		if (files == null) 
			return null; 
		for (int i = 0; i < files.length; i++) { 
			if (files[i].isDirectory()) { 
				refreshFileList(files[i].getAbsolutePath(), fileList); 
			} else { 
				fileList.add(files[i].getAbsolutePath());              
			} 
		} 
		return fileList;
	}
	
	/**
	 * 去掉目录最后一个/or\
	 * @param dir
	 * @return
	 */
	public String getDirName(String dir){
		if(dir != null && !dir.equals("")){
			if(dir.endsWith("/") || dir.endsWith("\\")){
				dir = dir.substring(0, dir.length()-1);
			}
		}
		return dir;
	}
	
	/**
	 * 穿件文件名
	 * @param idcHome
	 * @param constant
	 * @param typeCode
	 * @param oldFileName
	 * @param resultCode
	 * @return
	 */
	public String createFileName(String idcHome, String constant, String typeCode, String oldFileName, String resultCode){
		
		if (!idcHome.endsWith(File.separator)) {
			idcHome = idcHome + File.separator;
        }
		if(typeCode.endsWith(File.separator)){
			typeCode = typeCode.substring(0, typeCode.length()-1);
		}
		if(oldFileName.indexOf(File.separator) != -1){
			String[] nameArray = oldFileName.split("\\"+File.separator);
			oldFileName = nameArray[nameArray.length-1];
		}
		if(oldFileName.indexOf(".") != -1){
			String[] fileNameArray = oldFileName.split("\\.");
			oldFileName = fileNameArray[0];
		}
		return idcHome + constant + File.separator + typeCode + "-" + oldFileName + "-" + resultCode;
	}
	
	public static void main(String[] args) {
		System.out.println(new FileUtil().createFileName("D:\\lin\\ftp", "999", "2", "D:\\lin\\ftp\\2\\abc.xml", "3"));
		
	}
}
